<?php
class Admin_manageAction extends CommAction{
	public function __construct(){
		parent::__construct();
        $menuid =!empty($_REQUEST['menuid'])?intval($_REQUEST['menuid']):'';
        $nav_submenu=$this->submenu($menuid,false,-1);
        $this->assign('nav_submenu',$nav_submenu);
	}

    /**
     * lujinfa
     * 2013-09-04
     * 编辑个人信息
     */
    public  function  public_edit_info()
    {
        echo "修改人个信息";

    }

    public function public_edit_pwd()
    {
        echo "修改 密码";
    }
    /**
     * lujinfa
     * 2013-09-03
     * 添加管理员方法
     *
     */
    public function add(){
		if($_POST['dosubmit']){
			$admin_db = new Model('admin');
            $username = !empty($_POST['username'])?trim($_POST['username']):'';
            $truename = !empty($_POST['truename'])?trim($_POST['truename']):'';
            $password = !empty($_POST['password'])?trim($_POST['password']):'';
            $repass = !empty($_POST['password2'])?trim($_POST['password2']):'';
            $email = !empty($_POST['email'])?trim($_POST['email']):'';
            $roleid = !empty($_POST['roleid'])?trim($_POST['roleid']):'';

            if($username=='' || strlen($username)>18 || strlen($username)<6)
            {
                $this->error('用户名不能为空,长度最小6，最大18',U('Manager/add'));
            }
            if($truename =='' || strlen($truename)<6 || strlen($truename)>12)
            {
                $this->error('真实姓名不能为空,中文最少2个，最多4个',U('Manager/add'));
            }

            if($password =='' || strlen($password)<6 || strlen($password)<16)
            {
                $this->error('密码不能为空,密码最少6个，最长16个',U('Manager/add'));
            }
            else{
                if($password!=$repass)
                {
                    $this->error('两次输入密码不匹配',U('Manager/add'));
                }
            }

            $data['adminuser']=$username;
            $data['truename']=$truename;
            $data['password']=md5Encrypt($password);
            $data['adminemail']=$email;
            $data['roleid']=$roleid;
            $data['addtime']=date('Y-m-d H:i:s',time());

			if($admin_db->create($data)){
				if($admin_db->add($data)){
                    $this->success('添加成功',__APP_PATH__.'index.php?g=Admin&m=Manager&a=list');
				}else{
					$this->error('添加失败');
				}
			}else{
				$this->error($admin_db->getError());
			}
			
		}else{
			$role_db = new Model('role');
			$rolelist = $role_db->field('id,name')->select();
			$this->assign('rolelist',$rolelist);
			$this->display('addadmin');
		}
	}
	//后台管理员信息列表
	public function index(){

        //var_dump($_REQUEST);
			import('@.ORG.Util.Page');
			$admin = new Model('admin');

			$condition['del_state'] =0;
			$admin_count = $admin->where($condition)->count();
			$admin_page = new Page($admin_count,15);
			$admin_show = $admin_page->show();

            $role_db = new Model('role');
			$admin_list = $admin->field('adminid,adminuser,truename,adminemail,roleid,addtime')->where($condition)->limit($admin_page->firstRow.','.$admin_page->listRows)->select();
			foreach($admin_list as $adminval){
                $condition_role['id']=$adminval['roleid'];
                $role_info = $role_db->field('id,name')->where($condition_role)->find();
                $adminval['rolename']=$role_info['name'];

				$adminlist[]=$adminval;
			}
            $admin_db = new Model('admin');
            $condition_admin['adminid']=session('userid');
            $local_admin = $admin_db->field('*')->where($condition_admin)->find();
            /*var_dump($_SESSION);
            if($local_admin['roleid']!=1 && $roleid==1)
            {
                $this->error('非超级管理员，无权修改“超级管理员角色”');
            }
            */


			$this->assign('local_admin',$local_admin);
			$this->assign('adminlist',$adminlist);
			$this->assign('adminpage',$admin_show);
			$this->display('adminlist');
	}

    /**
     * lujinfa
     * 2013-09-03
     * 查看管理用户名是否有重复
     */
    public function  public_checkname_ajx()
    {
        //var_dump($_REQUEST);
        $ajx_val = !empty($_REQUEST['param'])?trim($_REQUEST['param']):'';
        $ajx_key = !empty($_REQUEST['name'])?trim($_REQUEST['name']):'';
        $admin_db = new Model('admin');
        $return_str ='';

        if($ajx_key=='username')
        {
            $condition_admin['adminuser']=$ajx_key;
            $get_admin_info = $admin_db->field('*')->where($condition_admin)->find();
            if(!$get_admin_info)
            {
                echo  '{"info":"验证通过！","status":"y"}';
            }
            else
            {
                echo  '{"info":"未验证通过！","status":"n"}';
            }
            //var_dump($get_admin_info);
        }


    }
	//删除admin表的数据
	public function delete(){
		$condition['adminid'] = $_GET['id'];
		$admin = new Model('admin');
		$adminlist = $admin->field('adminid,del_state')->where($condition)->find();
		if(!empty($adminlist)){
			if($admin->where($condition)->save(array('del_state'=>1))){
				$this->success('删除成功','__URL__/adminlist');
			}else{
				$this->error('删除信息失败','__URL__/adminlist');
			}
		}else{
			$this->error('删除用户不存在');
		}
	}
	//修改admin表信息内容
	public function edit() {
		if(session('userid') !=null){
			if($_POST['dosubmit']){
                $id  = !empty($_POST['id'])?intval(trim($_POST['id'])):0;
                if($id<=0)
                {
                    $this->error('修改用户有问题，请检查');
                }
                if($id==1)
                {
                    if(session('userid')!='admin')
                    {
                        $this->error('超级管理员密码只有超级管理才能修改');
                    }
                }
				$data['adminemail'] = trim($_POST['email']);
				$data['truename'] = trim($_POST['truename']);
				$data['roleid'] = trim($_POST['role']);
				$condition['adminid'] = $id;
				$admin = new Model('admin');
				if($admin->where($condition)->save($data)){
					$this->success('修改成功','__URL__/adminlist');
				}else{
					$this->error('修改失败','__URL__/updateadmin');
				}
				
			}else{
				$condition['adminid'] = trim($_GET['id']);
				$admin = new Model('admin');
				$adminlist = $admin->field('adminid,adminemail,truename,roleid')->where($condition)->find();
					switch($adminlist['roleid']){
						case 1: 
								$adminlist['rolename']='超级管理员';
								break;
						case 2:
								$adminlist['rolename']='管理员';
								break;
						case 3: 
								$adminlist['rolename']='用户';
								break;
						case 4:
								$adminlist['rolename']='竞价';
								break;
						case 5: 
								$adminlist['rolename']='关键词';
								break;
						case 6:
								$adminlist['rolename']='搜索引擎';
								break;
						case 7: 
								$adminlist['rolename']='采集';
								break;
						case 8:
								$adminlist['rolename']='财务';
								break;
						case 0:
								$adminlist['rolename']='财务';
								break;
						default:
								$adminlist['rolename']='禁止登录';
					}
				$role = new Model('role');
				$rolelist = $role->field('id,name')->select();
				$this->assign('rolelist',$rolelist);
				$this->assign('adminlist',$adminlist);
				$this->display('updateadmin');
			}
		}else{
				$this->error('请先登录','__APP__/Index/index');
			}
	}
	//修改管理员密码
	public function changepwd(){
		if($_POST['dosubmit']){
			if($_POST['newpwd'] == $_POST['repwd']){
				$condition['adminid'] = trim($_POST['id']);
				$admin = new Model('admin');
				$pass = $admin->where($condition)->getField('adminpass');
				if(md5($_POST['oldpwd']) == $pass){
					$data['adminpass'] = md5($_POST['newpwd']);
					if($admin->where($condition)->save($data)){
						$this->success('恭喜你，修改密码成功','__URL__/adminlist');
					}else{
						$this->error('修改失败，请重新修改','__URL__/changepwd');
					}
				}else{
					$this->error('当前密码错误','__URL__/changepwd');
				}
			}else{
				$this->error('两次密码不一致，请重新输入','__URL__/changepwd');
			}
		}else{
			$id = $_GET['id'];
			$this->assign('id',$id);
			$this->display('changepwd');
		}
	}

}
?>